//
//  GYDCameraAllAblumView.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/20.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit
import SnapKit

class GYDCameraAllAblumView: UIView, UITableViewDelegate, UITableViewDataSource {
    
    
    private var selectedBlcok : ((albumModel:TZAlbumModel?) -> ())?
    private var albumArr : [TZAlbumModel]?
    private var closeView : UIButton?
    private var contentView : UIView?
    private var tableView : UITableView?
    
    static func showWithAlbumArr(albumArr: [TZAlbumModel], selectedBlock:((albumModel:TZAlbumModel?) -> ())?) -> Void {
        let allAblumView = GYDCameraAllAblumView(frame: CGRect(x: 0, y: 0, width: GYDWindowWidth, height: GYDWindowHeight))
        UIApplication.sharedApplication().keyWindow?.addSubview(allAblumView)
        allAblumView.selectedBlcok = selectedBlock
        allAblumView.albumArr = albumArr
        allAblumView.show()
    }
    
    func show() -> Void {
        createBlackView()
        createContentView()
        showAniamtion()
    }
    
    private func createBlackView() -> Void {
        let view = UIButton(frame: self.bounds)
        closeView = view
        view.backgroundColor = RGBA(0, g: 0, b: 0, a: 0.1)
        view.addTarget(self, action: #selector(GYDCameraAllAblumView.dissmiss), forControlEvents: UIControlEvents.TouchUpInside)
        addSubview(view)
    }
    
    private func createContentView() -> Void {
        contentView = UIView(frame: CGRect(x: 0, y: 64 , width: GYDWindowWidth, height: GYDWindowHeight/2.0 + 50))
        contentView?.backgroundColor = UIColor.clearColor()
        contentView?.clipsToBounds = true
        self.addSubview(contentView!)
        
        let tale = UITableView(frame: CGRect(x: 0, y: -GYDWindowHeight/2.0 , width: GYDWindowWidth, height: GYDWindowHeight/2.0), style: UITableViewStyle.Plain)
        tale.registerClass(GYDCameraAllAblumCell.classForCoder(), forCellReuseIdentifier: "cellId")
        tale.delegate = self
        tale.dataSource = self
        tale.backgroundColor = UIColor.whiteColor()
        tale.rowHeight = 70
        tale.separatorStyle = UITableViewCellSeparatorStyle.None
        contentView!.addSubview(tale)
        tableView = tale
    }
    private func showAniamtion() -> Void {
        UIView.animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { 
            self.tableView?.top = 0
            }) { (isOver) in
                if isOver {
                    self.contentView?.height = (self.tableView?.height)!
                }
        }
    }
    
    @objc private func dissmiss() -> Void {
        
        if let tempBlock = selectedBlcok {
            tempBlock(albumModel: nil)
        }
        closeView?.removeFromSuperview()
        UIView.animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.tableView?.top = -GYDWindowHeight/2.0
        }) { (isOver) in
            self.removeFromSuperview()
        }
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.albumArr == nil {
            return 0
        }else {
            return (self.albumArr?.count)!
        }
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell : GYDCameraAllAblumCell = tableView.dequeueReusableCellWithIdentifier("cellId", forIndexPath: indexPath) as! GYDCameraAllAblumCell
        
        cell.updateWithModel((self.albumArr?[indexPath.row])!)
        
        return cell
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print(self.albumArr)
        let albumModel = self.albumArr![indexPath.row]
        if let tempBlock = selectedBlcok {
            tempBlock(albumModel: albumModel)
        }
        closeView?.removeFromSuperview()
        UIView.animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.tableView?.top = -GYDWindowHeight/2.0
        }) { (isOver) in
            self.removeFromSuperview()
        }
    }
    
}


class GYDCameraAllAblumCell: UITableViewCell {
    
    private var iconView : UIImageView?
    private var nameLabel : UILabel?
    private var numberLabel : UILabel?
    private lazy var imageManager : TZImageManager = {
        return TZImageManager()
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        createViews()
        
    }
    private func createViews() -> Void {
        let imageView1 = UIImageView()
        imageView1.contentMode = UIViewContentMode.ScaleAspectFill
        imageView1.clipsToBounds = true
        iconView = imageView1
        contentView.addSubview(imageView1)
        imageView1.snp_makeConstraints { (make) in
            make.left.equalTo(5)
            make.bottom.equalTo(-5)
            make.top.equalTo(5)
            make.width.equalTo(imageView1.snp_height)
        }
        
        let label1 = UILabel()
        label1.textColor = RGB(70, g: 70, b: 70)
        label1.font = UIFont.systemFontOfSize(17)
        nameLabel = label1
        contentView.addSubview(label1)
        label1.snp_makeConstraints { (make) in
            make.left.equalTo(imageView1.snp_right).offset(5)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }
        
        let label2 = UILabel()
        label2.textColor = RGB(120, g: 120, b: 120)
        label2.font = UIFont.systemFontOfSize(13)
        numberLabel = label2
        contentView.addSubview(label2)
        label2.snp_makeConstraints { (make) in
            make.left.equalTo(label1.snp_right).offset(5)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }
        
    }
    
    func updateWithModel(ablum : TZAlbumModel) -> Void {
        nameLabel?.text = ablum.name
        numberLabel?.text = String(format: "%ld", ablum.count)
        imageManager.getPostImageWithAlbumModel(ablum) { [unowned self] (image) in
            self.iconView?.image = image
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}

